package com.ruoyi.project.configureSystem.service.impl;

import java.util.*;

import com.ruoyi.common.exception.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.RandomTool;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.configureSystem.domain.CArea;
import com.ruoyi.project.configureSystem.domain.CCompany;
import com.ruoyi.project.configureSystem.domain.SysNode;
import com.ruoyi.project.configureSystem.mapper.CAreaMapper;
import com.ruoyi.project.configureSystem.mapper.CCompanyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.configureSystem.mapper.CStationMapper;
import com.ruoyi.project.configureSystem.domain.CStation;
import com.ruoyi.project.configureSystem.service.ICStationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 站点信息Service业务层处理
 *
 * @author wangshuo
 * @date 2021-04-12
 */
@Service
public class CStationServiceImpl implements ICStationService
{
    @Autowired
    private CStationMapper cStationMapper;
    @Autowired
    private CAreaMapper cAreaMapper;
    @Autowired
    private CCompanyMapper cCompanyMapper;

    /**
     * 查询站点信息
     *
     * @param id 站点信息ID
     * @return 站点信息
     */
    @Override
    public CStation selectCStationById(String id)
    {
        return cStationMapper.selectCStationById(id);
    }

    /**
     * 查询站点信息列表
     *
     * @param cStation 站点信息
     * @return 站点信息
     */
    @Override
    public List<CStation> selectCStationList(CStation cStation)
    {
        List<CStation> cStations = cStationMapper.selectCStationList(cStation);
        cStations.forEach(x->{
            CArea cArea = cAreaMapper.selectCAreaById(x.getAreaId());
            CCompany cCompany = cCompanyMapper.selectCCompanyById(x.getCompanyId());
            x.setAreaName(cArea.getAreaName());
            x.setCompanyName(cCompany.getCompanyName());
        });
        return cStations;
    }

    /**
     * 新增站点信息
     *
     * @param cStation 站点信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCStation(CStation cStation)
    {
        if(StringUtils.isBlank(cStation.getAreaId())||StringUtils.isBlank(cStation.getCompanyId())){
            throw new BaseException("请选择站点所属区域/公司");
        }
        cStation.setId("LDKS"+ RandomTool.generatorNum());
        cStation.setCreateTime(DateUtils.getNowDate());
        cStation.setCreateBy(SecurityUtils.getUsername());
        //父级列表需要拼接，因为不在同一个表中
        CArea cArea = cAreaMapper.selectCAreaById(cStation.getAreaId());
        CCompany cCompany = cCompanyMapper.selectCCompanyById(cStation.getCompanyId());
        String s= "";
        if(StringUtils.isNotBlank(cArea.getAncestors())){
            s = cArea.getId() + "," + cArea.getAncestors();
        }else {
            s = cArea.getId();
        }
        String s1 = "";
        if(StringUtils.isNotBlank(cCompany.getAncestors())){
            s1 = cCompany.getAncestors() + "," + cCompany.getId();
        }else {
            s1 = cCompany.getId();
        }
        cStation.setAncestors(s.trim()+","+s1.trim());
        return cStationMapper.insertCStation(cStation);
    }

    /**
     * 修改站点信息
     *
     * @param cStation 站点信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCStation(CStation cStation)
    {
        cStation.setUpdateBy(SecurityUtils.getUsername());
        cStation.setUpdateTime(new Date());
//        //父级列表需要拼接，因为不在同一个表中
//        CArea cArea = cAreaMapper.selectCAreaById(cStation.getAreaId());
//        CCompany cCompany = cCompanyMapper.selectCCompanyById(cStation.getCompanyId());
//        String areaancestors = cArea.getAncestors();
//        String s = areaancestors + "," + cArea.getId();
//        String comancestors = cCompany.getAncestors();
//        String s1 = comancestors + "," + cCompany.getId();
//        cStation.setAncestors(s+","+s1);
        if(StringUtils.isBlank(cStation.getAreaId())||StringUtils.isBlank(cStation.getCompanyId())){
            throw new BaseException("请选择站点所属区域/公司");
        }
        cStation.setId("LDKS"+ RandomTool.generatorNum());
        cStation.setCreateTime(DateUtils.getNowDate());
        cStation.setCreateBy(SecurityUtils.getUsername());
        //父级列表需要拼接，因为不在同一个表中
        CArea cArea = cAreaMapper.selectCAreaById(cStation.getAreaId());
        CCompany cCompany = cCompanyMapper.selectCCompanyById(cStation.getCompanyId());
        String s= "";
        if(StringUtils.isNotBlank(cArea.getAncestors())){
            s = cArea.getId() + "," + cArea.getAncestors();
        }else {
            s = cArea.getId();
        }
        String s1 = "";
        if(StringUtils.isNotBlank(cCompany.getAncestors())){
            s1 = cCompany.getAncestors() + "," + cCompany.getId();
        }else {
            s1 = cCompany.getId();
        }
        cStation.setAncestors(s.trim()+","+s1.trim());
        return cStationMapper.updateCStation(cStation);
    }

    /**
     * 批量删除站点信息
     *
     * @param ids 需要删除的站点信息ID
     * @return 结果
     */
    @Override
    public int deleteCStationByIds(String[] ids)
    {
        return cStationMapper.deleteCStationByIds(ids);
    }

    /**
     * 删除站点信息信息
     *
     * @param id 站点信息ID
     * @return 结果
     */
    @Override
    public int deleteCStationById(String id)
    {
        return cStationMapper.deleteCStationById(id);
    }

    @Override
    public List<SysNode> stationTree(CStation cStation) {
        List<CStation> cStations = cStationMapper.selectCStationList(cStation);
        //不在同一张表需要特殊处理成树结构
        Set<SysNode> sysNodes = new HashSet<>();
        cStations.forEach(x->{
            if(StringUtils.isNotBlank(x.getAreaId())&&StringUtils.isNotBlank(x.getCompanyId())){
                CCompany cCompany = cCompanyMapper.selectCCompanyById(x.getCompanyId());
                String a1= "";
                if(cCompany!=null){
                    if(StringUtils.isNotBlank(cCompany.getAncestors())){
                        a1 = cCompany.getId()+","+cCompany.getAncestors();
                    }else {
                        a1 = cCompany.getId();
                    }
                }
                String a2= "";
                String at2 = "";
                CArea cArea = cAreaMapper.selectCAreaById(x.getAreaId());
                if(cArea!=null){
                    //区域上级为公司，区域向上取唯一父节点，作为虚拟节点的前缀
                    String[] split1 = a1.split(",");
                    if(StringUtils.isNotBlank(cArea.getAncestors())){
                        a2 = cArea.getId()+","+cArea.getAncestors();
                    }else {
                        a2= cArea.getId();
                    }
                    String[] split = a2.split(",");
                    String[] tmp = Arrays.stream(split).map(z -> {
                        return split1[0].substring(4) + "-" + z;
                    }).toArray(String[]::new);
                    at2 = StringUtils.join(tmp, ",");
                }
                String s= x.getId()+","+at2 +","+a1;
//                String s = x.getId()+","+x.getAncestors();
                String[] split = s.split(",");
                for (int i = 0; i < split.length; i++) {
                    SysNode node = new SysNode();
                    node.setNodeId(split[i]);
                    if((i+1)<split.length){
                        node.setParentId(split[i + 1]);

                    }else {
                        node.setParentId("0");
                    }
                    sysNodes.add(node);
                }
            }
        });
        sysNodes.forEach(y->{
            if(y.getNodeId().startsWith("LDKS")){
                CStation cStation1 = cStationMapper.selectCStationById(y.getNodeId());
                y.setNodeName(cStation1.getStationName());
                y.setIsStation(1);
            }else if(y.getNodeId().contains("A")){
                String[] split = y.getNodeId().split("-");
                CArea cArea = cAreaMapper.selectCAreaById(split[1]);
                y.setNodeName(cArea.getAreaName());
                y.setIsStation(0);
            }else if(y.getNodeId().startsWith("C")){
                CCompany cCompany = cCompanyMapper.selectCCompanyById(y.getNodeId());
                y.setNodeName(cCompany.getCompanyName());
                y.setIsStation(0);
            }
            //
//            if(y.getParentId().equalsIgnoreCase("ldks")){
//                CStation cStation1 = cStationMapper.selectCStationById(y.getParentId());
//                y.setParentName(cStation1.getStationName());
//            }else if(y.getParentId().equalsIgnoreCase("a")){
//                CArea cArea = cAreaMapper.selectCAreaById(y.getParentId());
//                y.setParentName(cArea.getAreaName());
//            }else if(y.getParentId().equalsIgnoreCase("com")){
//                CCompany cCompany = cCompanyMapper.selectCCompanyById(y.getParentId());
//                y.setParentName(cCompany.getCompanyName());
//            }

        });
        return new ArrayList<>(sysNodes);
    }
}
